<?php

require_once 'api/apiClient.php';
require_once 'api/contrib/apiPlusService.php';

session_start();

$client = new apiClient();
$client->setApplicationName("Google+ PHP Starter Application");
$client->setScopes(array('https://www.googleapis.com/auth/plus.me'));
$plus = new apiPlusService($client);

if (isset($_REQUEST['logout'])) {
  unset($_SESSION['access_token']);
}

if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['access_token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['access_token'])) {
  $client->setAccessToken($_SESSION['access_token']);
}

if ($client->getAccessToken()) {
  $me = $plus->people->get('me');

  $url = filter_var($me['url'], FILTER_VALIDATE_URL);
  $img = filter_var($me['image']['url'], FILTER_VALIDATE_URL);
  $name = filter_var($me['displayName'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
  $personMarkup = "<a rel='me' href='$url'>$name</a><div><img src='$img'></div>";

  $optParams = array('maxResults' => 100);
  $activities = $plus->activities->listActivities('me', 'public', $optParams);
  $activityMarkup = '';
  foreach($activities['items'] as $activity) {
    $url = filter_var($activity['url'], FILTER_VALIDATE_URL);
    $title = filter_var($activity['title'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
    $content = filter_var($activity['object']['content'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
    $activityMarkup .= "<div class='activity'><a href='$url'>$title</a><div>$content</div></div>";
  }

  $_SESSION['access_token'] = $client->getAccessToken();
} else {
  $authUrl = $client->createAuthUrl();
}
?>
<head>
<meta http-equiv="Content-Language" content="en-us">
<script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>
<script type = "text/javascript">

	function hideWelcome() {
		document.getElementById("welcome").style.display="none"; 
		document.getElementById("connect").style.display="block";
		document.getElementById("vote_id").style.display="block";
	}
	function startTimer() {
		var tim = window.setTimeout("hideWelcome()", 1000);
	}
	function checkID() {
		$.getJSON("/backend/query.php?id="+$("#voter_id").val()+"&pid="+$("#voter_parent_id").val()+"&mname="+$("#voter_mother_fullname").val(), 
		function(data){
			if ('token' in data) {
				localStorage.setItem("token", data.token);
				document.getElementById("vote_id").style.display="none";
				document.getElementById("vote_panel").style.display="block";
			} else {
				alert(data.error);
			}
		});
	}
	function voteID() {
		$.getJSON("/backend/vote.php?token="+localStorage["token"]+"&v="+$("input[name='voter']:checked").val(), 
		function(data){
			if ('succeed' in data) {
				localStorage.removeItem("token");
				document.getElementById("vote_panel").style.display="none";
				document.getElementById("social").style.display="block";
			} else {
				alert(data.error);
			}
		});

		
	}
</script>
</head>
<body onload="startTimer()">

<div id="welcome" style="display:block">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="100%">
	<tr>
		<td>
		<p align="center" style="margin-top: 0; margin-bottom: 0">
		<img border="0" src="flag_Egypt.png" width="256" height="256"></p>
		<p align="center" style="margin-top: 0; margin-bottom: 0"><b><font face="Verdana" size="5">Egyptian Elections</font></b><p align="center" style="margin-top: 0; margin-bottom: 0">&nbsp;<p align="center" style="margin-top: 0; margin-bottom: 0">
		<b><font face="Tahoma" size="2" color="#666666">Please wait ...</font></b></td>
	</tr>
</table>
</div>

<div id="connect" style="display:none">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="100%">
	<?php if(!isset($authUrl)) : ?>
	<tr>
		<td width="20%" align="left" valign="top">
		<p style="margin-top: 5px; margin-bottom: 0"><b>
		<font face="Arial" color="#333333">
		<img border="0" src="<?php echo $img; ?>" width="50" height="50" align="left"> 
		Welcome</font></b></p>
		<p style="margin-top: 5px; margin-bottom: 0"><b>
		<font face="Arial" color="#333333"><?php echo $name; ?></font></b></td>
		
		<td>
		<div id='vote_id' style="display:none">
		<form method="POST" action="?">
			<p align="center" style="margin-top: 10px; margin-bottom: 10px">
			<img border="0" src="id-2.png" width="303" height="190"></p>
			<p align="center" style="margin-top: 10px; margin-bottom: 10px">
			National ID Number: <input type="text" id="voter_id" name="voter_id" size="26"></p>
			<p align="center" style="margin-top: 10px; margin-bottom: 10px">
			Mother Full Name: <input type="text" id="voter_mother_fullname" name="voter_mother_fullname" size="24"></p>
			<p align="center" style="margin-top: 10px; margin-bottom: 10px">
			Parent ID Number: 
			<input type="text" id="voter_parent_id" name="voter_parent_id" size="24"></p>
			<p align="center" style="margin-top: 10px; margin-bottom: 10px">
			<input type="button" value="Check ID owner" name="check_id" onclick="checkID();"></p>
		</form>		
		</div>
		
		<div id='vote_panel' style="display:none">
		<form id="vote_form">
		<p align="center">
		<img border="0" src="morsy.jpg" width="152" height="276"></p>
		<p align="center"><input type="radio" value="1" checked name="voter">Ahmed 
		Shafeek</p>
		<p align="center">
		<img border="0" src="shafee2.jpg" width="152" height="276">
		<p align="center">
		<input type="radio" name="voter" value="2">Mohamed Morsy<p align="center">
		<input type="button" value="Vote" name="vote" onclick='voteID();'></form>
		</div>
		
		<div id='social' style="display:none">
		<p align="center"><b><font size="4" face="Verdana" color="#333333">You 
		have successfully voted for the presidency elections 2016</font></b></p>
		<p align="center"><b><font size="4" face="Verdana" color="#333333">Ask 
		your friends about the elections through hangouts</font></b></p>
		<p align="center"><a href="https://plus.google.com/hangouts/_?gid=egypt-elections" style="text-decoration:none;">
  		<img src="https://ssl.gstatic.com/s2/oz/images/stars/hangout/1/gplus-hangout-60x230-normal.png"
    	alt="Start a Hangout" style="border:0;width:230px;height:60px;"/></a><p align="center">&nbsp;<p align="center">
		<b><font face="Tahoma" color="#FF0000">Please don't forget to logout</font></b><p align="center">&nbsp;</div>
		</td>
		
		<td width="20%" align="right" valign="top">
		<p style="margin-left:10px; margin-right:10px; margin-top:5px; margin-bottom:10px"><b><font face="Tahoma" color="#333333">
		<a href="?logout"><font color="#333333">
		<span style="text-decoration: none">Logout</span></font></a></font></b></td>
	</tr>
	<?php else: ?>
	<tr>
		<td colspan="3">
		<p align="center" style="margin-top: 0; margin-bottom: 0"><b>
		<font size="4" face="Verdana" color="#333333"><a href="<?php echo $authUrl; ?>">
		<font color="#333333"><span style="text-decoration: none">Login using 
		your Google Account</span></font></a></font></b></p>
		<p align="center" style="margin-top: 0; margin-bottom: 0">
		<a href="<?php echo $authUrl; ?>">
		<span style="text-decoration: none">
		<img border="0" src="elections.jpg" width="510" height="163"></span></a></td>
	</tr>
	<?php endif ?>
</table>
</div>

</body>